let text = require('../../../../common/util/text.js')
let util = require('../../../../common/util/util.js')
const app = getApp()
Page({
    data: {
        week: '',
        day: '',
        Page:1,
        step:false,
        treatment_item_id:'',
        pain_date:'',
        begin_time:'请选择开始疼痛时间',
        pain_duration:'',
        pain_duration_unit:1,// 疼痛时长单位
        PickerRangeUnit:['分钟','小时'],
        writing_template:[{
          temp_title:'最低疼痛评分(0-10分)',
          temp_content:'想想你最不痛苦的时刻，你会给疼痛评分多少？',
          type:1,
        },{
          temp_title:'最高疼痛评分(0-10分)',
          temp_content:'回想一下你今天感到最痛的一次，你会给疼痛评分多少？',
          type:1,
        },{
          temp_title:'平均疼痛评分(0-10分)',
          temp_content:'在一天中，你认为你大部分时间所经历的疼痛程度是多少？',
          type:1,
        },{
          temp_title:'疼痛带给我的困扰(0-10分)',
          temp_content:'',
          type:1,
        },{
          temp_title:'诱发疼痛的日常活动',
          temp_content:'',
          type:2,
        }],
        form:[-1,-1,-1,-1],
        is_temporary_medication:'是否服药',
        is_relieve_pain_drug:'是否减轻',
        pain_type:[],
        PickerRange:[{name:'胀痛',check:false},
        {name:'刺痛',check:false},{name:'灼烧痛',check:false},{name:'抽痛',check:false},
        {name:'钝痛',check:false},{name:'串痛',check:false},{name:'压痛',check:false},
        {name:'坠痛',check:false},{name:'冷痛',check:false},{name:'过电样痛',check:false},
        {name:'锐痛',check:false},{name:'麻痛',check:false},{name:'射击痛',check:false},
        {name:'揪心痛',check:false},{name:'搏动样痛',check:false},{name:'撕裂痛',check:false},
        {name:'牵拉痛',check:false},{name:'麻胀痛',check:false},{name:'麻痒痛',check:false},
        {name:'牵涉痛',check:false},{name:'其他',check:false},],
        text_tip:['无痛','无痛','无痛','无痛'],
        bgColor:['#eee','#eee','#eee','#eee'],
        bgColorS:['linear-gradient(to right, #23E201 10% , #FF4F66 100%)','linear-gradient(to right, #23E201 10% , #FF4F66 100%)','linear-gradient(to right, #23E201 10% , #FF4F66 100%)','linear-gradient(to right, #23E201 10% , #FF4F66 100%)'],
        pain_part:[],
    },
    onShow:function(){
      getApp().globalData.lock=false
      this.setData({
        pain_date:util.formatDate_chinese(new Date()),
      })
    },
    onLoad: function (opt) {
      this.setData({
        week: Number(opt.week),
        day:Number(opt.day),
        index:Number(opt.index),
        treatment_item_id:opt.treatment_item_id,
        type:Number(opt.type)
      })
      this.xToPracticeStatus()
      this.xGetHelp()
    },
    xChildReload: function () {
      var pages = getCurrentPages();//当前页面栈
      if (pages.length > 1) {
        var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
        beforePage.xInfo();//触发父页面中的方法
      }
    },
    xPicker:function(e){
      let that = this
      let data = e.currentTarget.dataset.data
      let index = e.currentTarget.dataset.index
      for(let index in that.data.pain_type){
        if(that.data.pain_type[index] == data){
            for(let i in that.data.PickerRange){
              if(that.data.PickerRange[i].name==data){
                  let current = i
                  this.setData({
                    ['PickerRange['+current+'].check']:false
                  })
                }
            }
            that.data.pain_type.splice(index,1)
            this.setData({
              pain_type:that.data.pain_type,
              // ['PickerRange['+index+'].check']:false
            })
            console.log(that.data.pain_type)
            return
        }
      }
      that.data.pain_type.push(data)
      
      this.setData({
        pain_type:that.data.pain_type,
        ['PickerRange['+index+'].check']:true
      })
    },
    // chooseparts:function(e){
    //   let that =this
    //   let name = e.currentTarget.dataset.name
    //   let current = e.currentTarget.dataset.index
    //   for(let index in that.data.pain_part){
    //     if(that.data.pain_part[index] == name){
    //         that.data.pain_part.splice(index,1)
    //         this.setData({
    //           pain_part:that.data.pain_part
    //         })
    //         return
    //     }
    //   }
    //   that.data.pain_part.push(name)
    //   this.setData({
    //     pain_part:that.data.pain_part,
    //     current:current
    //   })
    // },
    setpartsC:function(e){
      let current = e.currentTarget.dataset.index
      this.setData({
        partscurrent:current
      })
    },
    xNext:function(){
      let that =this
      for(let index in that.data.form){
        if(that.data.form[index]<0){
          wx.showModal({
            title: "提示",
            content: "请确保所有内容填写正确，当前有内容未填写，请检查！",
            showCancel: false,
            confirmText:'好的',
          })
          return
        }
      }
      // || that.data.pain_part.length==0
      if(that.data.begin_time =='请选择开始疼痛时间'|| that.data.pain_duration=='' || that.data.pain_type.length==0 || that.data.daily_activities=='' || that.data.pain_duration_unit=='' ){
        wx.showModal({
          title: "提示",
          content: "请确保所有内容填写正确，当前有内容未填写，请检查！",
          showCancel: false,
          confirmText:'好的',
        })
        return
      }
      this.setData({
        Page:2
      })
      wx.pageScrollTo({
        scrollTop: 0,
      })
    },
    xSetStep:function(){
      this.setData({
        step:!this.data.step
      })
      wx.pageScrollTo({
        duration: 0,
        scrollTop:0
      })
    },
    bgColorChange:function(e){
      let index = e.currentTarget.dataset.index
      this.setData({
        ['bgColor['+index+']']:'linear-gradient(to right,#D3F9CC 30%,#F2E5BF 60%,#F2CFD4 100%)'
      })
    },
   
    bindUnitChange:function(e){
      this.setData({
        pain_duration_unit:Number(e.detail.value)+1
      })
    },
    sliderchange:function(e){
      let that = this
      let data = e.detail.value
      let index = e.currentTarget.dataset.index
      that.setData({
        ['form['+index+']']: e.detail.value,
      })
      if(e.detail.value<=2){
        that.setData({
          ['bgColorS['+index+']']:'linear-gradient(to right,#23E201 50%,#97E201 100%)'
        })
      }else if( e.detail.value>2 && e.detail.value <=5){
        that.setData({
          ['bgColorS['+index+']']:'linear-gradient(to right,#23E201 10%,#FFBB00 100%)'
        })
      }else{
        that.setData({
          ['bgColorS['+index+']']:'linear-gradient(to right,#23E201 20%,#FFBB00 60%,#FF4F66 100%)'
        })
      }
      that.gettext(index,data)
    },
    gettext(index,data){
      let that=this;
      let idx = index
      let v =data
      switch(v) {
        case 0:
          that.setData({
            ['text_tip['+idx+']']: '无痛',
            ['left['+idx+']']: '5'
          })
        break;
        case 1:
          that.setData({
            ['text_tip['+idx+']']: '微弱疼痛',
            ['left['+idx+']']: '5'
          })
        break;
        case 2:
          that.setData({
            ['text_tip['+idx+']']: '轻微疼痛',
            ['left['+idx+']']: '8'
          })
        break;
        case 3:
          that.setData({
            ['text_tip['+idx+']']: '温和疼痛',
            ['left['+idx+']']: '15'
          })
        break;
        case 4:
          that.setData({
            ['text_tip['+idx+']']: '影响睡眠',
            ['left['+idx+']']: '20'
          })
        break;
        case 5:
          that.setData({
            ['text_tip['+idx+']']: '明显疼痛',
            ['left['+idx+']']: '30'
          })
        break;
        case 6:
          that.setData({
            ['text_tip['+idx+']']: '持续疼痛',
            ['left['+idx+']']: '40'
          })
        break;
        case 7:
          that.setData({
            ['text_tip['+idx+']']: '难以入睡',
            ['left['+idx+']']: '50'
          })
        break;
        case 8:
          that.setData({
            ['text_tip['+idx+']']: '难以忍受',
            ['left['+idx+']']: '60'
          })
        break;
        case 9:
          that.setData({
            ['text_tip['+idx+']']: '强烈疼痛',
            ['left['+idx+']']: '70'
          })
        break;
        case 10:
          that.setData({
            ['text_tip['+idx+']']: '剧烈疼痛',
            ['left['+idx+']']: '73'
          })
        break;
        default:
          return ''
      } 
    },
    input: function ( e ) {
        let that = this
        let data = e.detail.value
        let index = e.currentTarget.dataset.index
        that.setData({
          daily_activities: e.detail.value
        })
    },
    inputdrug_name:function(e){
      this.setData({
        drug_name:e.detail.value
      })
    },
    inputdrug_dose:function(e){
      this.setData({
        drug_dose:e.detail.value
      })
    },
    inputpain_duration:function(e){
      this.setData({
        pain_duration:e.detail.value
      })
    },
    inputadverse_reactions:function(e){
      this.setData({
        adverse_reactions:e.detail.value
      })
    },
    inputways_relieve_pain:function(e){
      this.setData({
        ways_relieve_pain:e.detail.value
      })
    },
    xSub: function () {
      let that=this;
      if(getApp().globalData.lock==true){
        return
      }
      if(that.data.is_temporary_medication || that.data.is_temporary_medication =='是否服药'){
        if(that.data.drug_name=='' || that.data.drug_dose=='' || that.data.is_relieve_pain_drug =='是否减轻'|| that.data.ways_relieve_pain==''){
          wx.showModal({
            title: "提示",
            content: "请确保所有内容填写正确，当前有内容未填写，请检查！",
            showCancel: false,
            confirmText:'好的',
          })
          return
        }
      }else if( !that.data.is_temporary_medication){
        if(that.data.ways_relieve_pain==''){
          wx.showModal({
            title: "提示",
            content: "请确保所有内容填写正确，当前有内容未填写，请检查！",
            showCancel: false,
            confirmText:'好的',
          })
          return
        }
      }
      let dataObj = {
          treatment_item_id:that.data.treatment_item_id,
          min_pain_score:that.data.form[0],
          max_pain_score:that.data.form[1],
          mean_pain_score:that.data.form[2],
          pain_distress:that.data.form[3],
          daily_activities:that.data.daily_activities,
          week:that.data.week,
          is_temporary_medication:that.data.is_temporary_medication,
          drug_name:that.data.drug_name,
          drug_dose:that.data.drug_dose,
          adverse_reactions:that.data.adverse_reactions,
          ways_relieve_pain:that.data.ways_relieve_pain,
          pain_type:that.data.pain_type,
          is_relieve_pain_drug:that.data.is_relieve_pain_drug,
          pain_date:that.data.pain_date,
          begin_time:that.data.begin_time,
          pain_duration:that.data.pain_duration,
          pain_duration_unit:that.data.pain_duration_unit,
          // pain_part:that.data.pain_part
      }
        wx.showModal({
            title: '确定保存吗？',
            cancelColor: '#9a9a9a',
            confirmColor: '#BC91F2',
            confirmText: '确定',
            success: res => {
              if (res.confirm) {
                getApp().globalData.lock=true
                wx.showLoading({
                  title: '保存中',
                })
                wx.cloud.callFunction({
                  name: 'digital_therapy',
                  data: {
                      action: "add_pain_diary",
                      dataObj:dataObj
                  }
                }).then(res => {
                  wx.hideLoading()
                  getApp().globalData.lock=false
                  wx.navigateBack({
                    delta:1
                  })
                  that.xChildReload()
                }).catch(err=>{
                  wx.hideLoading()
                  getApp().globalData.lock=false
                })
              } 
            }
        })
    },
    xToPracticeStatus:function(){
      let that =this;
      wx.cloud.callFunction({
        name: 'practice_help',
        data: {
          action: "get_practice_help_status",
          dataObj: {
            num:6
          }
        }
      }).then(res => {
        for(let index in res.result){
          if(res.result[index]._id==1){
            that.setData({
              PracticeStatus:res.result[index].status
            })
          }
        }
      })
    },
    xGetHelp:function(){
      let _dataObj = {
        num:6,//练习任务类型数值
        type:1,//1:练习说明
      }
      wx.cloud.callFunction({
        name: 'practice_help',
        data: {
          action: "get_practice_help",
          dataObj: _dataObj
        }
      }).then(res => {
          res.result[0].content=text.text(res.result[0].content)
          this.setData({
            practice_help:res.result[0]
          })
      })
    },
    xTosetM:function(e){
      wx.setStorageSync(this.data.treatment_item_id, this.data.treatment_item_id)
      this.setData({
        showtips:!this.data.showtips
      })
    },
    bindDateChange:function(e){
      this.setData({
        pain_date :e.detail.value
      })
    },
    bindStartChange:function(e){
      this.setData({
        begin_time :e.detail.value
      })
    },
    bindEndChange:function(e){
      this.setData({
        pain_duration :e.detail.value
      })
    },
    choose:function(e){
      this.setData({
        isIndex:e.currentTarget.dataset.index,
        is_temporary_medication:e.currentTarget.dataset.index==0 ?true:false
      })
    },
    choose_lighten:function(e){
      this.setData({
        isIndex_lighten:e.currentTarget.dataset.index,
        is_relieve_pain_drug:e.currentTarget.dataset.index==0 ?true:false
      })
    },
})